home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 4 / The Pier Shareware #4 (The Pier Exchange) (1994).ISO / 038 / prochook.exe / SCW.C < prev    next >
C/C++ Source or Header  |  1994-01-01  |  5KB  |  179 lines

  1. /*
  2.   SCW.C - Copyright (c) 1993 James M. Finnegan, All Rights Reserved
  3. */
  4. #include <windows.h>
  5. #include "scw.h"
  6. #include "prochook.h"
  7. #include "goodies.h"
  8.  
  9. // Global stuff
  10. HANDLE hInst;
  11. HWND   ghWnd;
  12. // Magic cookie for SetClassWord hook
  13. NPHOOKCHILD npHookChild;
  14.  
  15. // This structure array associates the possible values of the nIndex
  16. // parameter of SetClassWord() to a string.
  17. struct ClassItems
  18. {
  19.     int  nIndex;
  20.     char szText[20];
  21. }CI[]=
  22. {
  23.  
  24.     GCW_HBRBACKGROUND  ,"Background",
  25.     GCW_HCURSOR        ,"Cursor",
  26.     GCW_HICON          ,"Icon",
  27.     GCW_HMODULE        ,"Module",
  28.     GCW_CBWNDEXTRA     ,"WndExtra",
  29.     GCW_CBCLSEXTRA     ,"ClassExtra",
  30.     GCL_WNDPROC        ,"WndProc",
  31.     GCW_STYLE          ,"Style",
  32.     NULL               ,""
  33. };    
  34.  
  35.  
  36. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine,
  37.            int nCmdShow)
  38. {
  39.     static char szAppName[]="scw";
  40.     HWND        hwnd;
  41.     MSG         msg;
  42.     WNDCLASS    wndclass;
  43.  
  44.     
  45.     hInst=hInstance;
  46.     
  47.     if(!hPrevInstance)
  48.     {
  49.         wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  50.         wndclass.lpfnWndProc   = WndProc;
  51.         wndclass.cbClsExtra    = 0;
  52.         wndclass.cbWndExtra    = DLGWINDOWEXTRA;
  53.         wndclass.hInstance     = hInstance;
  54.         wndclass.hIcon         = LoadIcon(hInstance,szAppName);
  55.         wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  56.         wndclass.hbrBackground = COLOR_WINDOW + 1;
  57.         wndclass.lpszMenuName  = NULL;
  58.         wndclass.lpszClassName = szAppName;
  59.  
  60.         if(!RegisterClass(&wndclass))
  61.             return -1;
  62.     }
  63.  
  64.     if((hwnd=CreateDialog(hInstance,szAppName,0,NULL)) == NULL)
  65.         return -1;
  66.  
  67.     // Make HWND global
  68.     ghWnd=hwnd;  
  69.     
  70.     ShowWindow(hwnd,nCmdShow);
  71.  
  72.     while(GetMessage(&msg,NULL,0,0))
  73.     {
  74.         if((!IsWindow(hwnd)) ||
  75.            (!IsDialogMessage(hwnd,&msg)))
  76.         {
  77.             TranslateMessage(&msg);
  78.             DispatchMessage(&msg);
  79.         }
  80.     }
  81.  
  82.     return msg.wParam;
  83. }
  84.  
  85.  
  86. long WINAPI WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
  87. {
  88.     FARPROC lpfnNewSetClass; // Pointer for MakeProcInstance()
  89.     int     iTabStop[3];     // Tabs for the list box
  90.  
  91.         
  92.     switch(message)
  93.     {
  94.         case WM_CREATE:
  95.             //Center the window on the screen
  96.             CenterWindow(hWnd);
  97.             
  98.             // Set up the hook to our new function
  99.             lpfnNewSetClass=MakeProcInstance((FARPROC)NewSetClassWord,hInst);
  100.             npHookChild=SetProcAddress((FARPROC)SetClassWord,lpfnNewSetClass,FALSE);
  101.  
  102.             // Post a message to set the tabs in the list box
  103.             PostMessage(hWnd,WM_SETLBTABS,0,0L);
  104.             break;
  105.         
  106.         case WM_SETLBTABS:
  107.             iTabStop[0]=58;
  108.             iTabStop[1]=122;
  109.             iTabStop[2]=187;
  110.             SendDlgItemMessage(hWnd,IDL_MAINBOX,LB_SETTABSTOPS,3,(LONG)(LPINT)iTabStop);
  111.             
  112.             SetFocus(GetDlgItem(hWnd,IDB_CLEAR));
  113.             break;
  114.             
  115.         // Processing for the listbox buttons...
  116.         case WM_COMMAND:
  117.             switch(wParam)
  118.             {
  119.                 case IDB_CLEAR:
  120.                     SendDlgItemMessage(hWnd,IDL_MAINBOX,LB_RESETCONTENT,0,0L);
  121.                     break;
  122.  
  123.                 case IDB_EXIT:
  124.                     PostMessage(hWnd,WM_CLOSE,0,0L);
  125.                     break;
  126.             }
  127.             break;
  128.             
  129.         case WM_DESTROY:
  130.             // Delete the reference to the hook
  131.             SetProcRelease(npHookChild);
  132.             PostQuitMessage(0);
  133.             break;
  134.  
  135.         default:
  136.             return DefWindowProc(hWnd, message, wParam, lParam);
  137.             break;
  138.     }
  139.     return 0L;
  140. }
  141.  
  142. /* 
  143.  All the stuff below is called only when SetClassWord() is called from
  144.  another application! 
  145. */
  146.  
  147. WORD WINAPI NewSetClassWord(HWND hWnd, int nIndex, WORD wNewWord)
  148. {
  149.    WORD        i;
  150.    static char szLBText[50];
  151.    static char szClassName[20];
  152.    
  153.    // Find the text of nIndex
  154.    for(i=0;i<8;i++)
  155.    {
  156.        if(nIndex == CI[i].nIndex)
  157.            break;
  158.    }
  159.    
  160.    // Get the text name for this class
  161.    GetClassName(hWnd,szClassName,19);
  162.  
  163.    // Create the string for the listbox
  164.    wsprintf(szLBText,"%s\t%s\t%s\t%s",GetTaskName(GetCurrentTask()),
  165.                                       GetModuleName(GetClassWord(hWnd,GCW_HMODULE)),
  166.                                       (LPSTR)szClassName,(LPSTR)CI[i].szText);
  167.    
  168.    SendDlgItemMessage(ghWnd,IDL_MAINBOX,LB_ADDSTRING,0,(LONG)(LPSTR)szLBText);
  169.  
  170.     // Unhook us!
  171.    ProcUnhook(npHookChild);
  172.     // Reissue the call to SetClassWord()
  173.    i=SetClassWord(hWnd,nIndex,wNewWord);
  174.     // Rehook us!
  175.    ProcHook(npHookChild);
  176.    
  177.    return i;
  178. }     
  179.